package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.detailtab.dto.RemindDTO;
import com.xbongbong.pro.detailtab.vo.RemindVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.result.pojo.dto.PanoramaDTO;
import com.xbongbong.pro.statistic.result.pojo.vo.PanoramaVO;
import com.xbongbong.pro.statistic.service.ChartPanoramaService;
import com.xbongbong.pro.statistic.service.RemindService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 全景图
 * @author zcp
 * @version v1.0
 * @date 2019/5/30 14:51
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chart/panorama")
public class ChartPanoramaController {
    @Resource
    private ChartPanoramaService chartPanoramaService;
    @Resource
    private RemindService remindService;

    /**
     * 客户全景图
     *
     * @param panoramaDto 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zcp
     * @date 2019/5/30 15:19
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/customer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customer(@RequestBody @Valid PanoramaDTO panoramaDto, BindingResult br) {
        XbbResponse<PanoramaVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PanoramaVO panoramaVo = chartPanoramaService.customer(panoramaDto);
                response = new XbbResponse<>(panoramaVo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 全景图提示(客户)
     *
     * @param remindDTO 入参
     * @param br 处理错误
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/5/31 16:40
     * @since v1.0
     */
    @RequestMapping(value = "/remind/customer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRemindCustomer(@RequestBody @Valid RemindDTO remindDTO, BindingResult br) {
        XbbResponse<RemindVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RemindVO remindVO = remindService.remindCustomer(remindDTO);
                response = new XbbResponse<>(remindVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
